import React, { PureComponent } from 'react';
import ReactUeditor from 'ifanrx-react-ueditor';
import "./index.less"

import { Spin } from "antd";

class Neditor extends PureComponent{
    constructor(props){
        super(props);
        this.state={
            sploading:false,
            progress:this.props.progress?this.props.progress:-1
        }
    }
    render(){
        const { content, multipleImagesUpload, plugins, config } = this.props;
        const { sploading, progress } = this.state;
        return (
            <Spin spinning={sploading}>
                <ReactUeditor
                    ueditorPath={`/js/plugins/ueditor`}
                    value = { content }
                    plugins={ plugins }
                    config = {config}
                    onChange={ this.handleChange }
                    uploadImage = { this.uploadImageS }
                    multipleImagesUpload={ multipleImagesUpload }
                    progress={progress}
                    uploadVideo={this.uploadVideoS}
                />
            </Spin>
        )
    }

    handleChange = (content)=>{
        const { handleChange } = this.props;
        handleChange(content);
    }

    uploadImageS = async (e)=>{
        const { uploadImage } = this.props;
        this.setState(()=>({
            sploading:true
        }));
        let images = await uploadImage(e);
        return new Promise((resolve, reject)=>{
            resolve(images[0]);
            this.setState({
                sploading:false
            })
        })
    }

    uploadVideoS = async (e) => {
        const { uploadVideo } = this.props;

        let i = 0
        let instance = setInterval(() => {
            if (i !== 100) {
                this.setState({progress: ++i})
            }
        }, 1000)
        let videos = await uploadVideo(e);
        this.setState({progress: -1})
        clearInterval(instance)

        return new Promise((resolve, reject)=>{
            resolve(videos[0]);
        })
        // let _this = this
        // return new Promise(function(resolve, reject) {
        //     let i = 0
        //     let instance = setInterval(() => {
        //         if (i !== 100) {
        //             _this.setState({progress: ++i})
        //         }
        //     }, 50)
        //     setTimeout(() => {
        //             //resolve('https://cloud-minapp-1131.cloud.ifanrusercontent.com/1eBb1SeNlayvGEKT.mp4')
        //             _this.setState({progress: -1})
        //             clearInterval(instance)
        //     }, 5100)
        // })
    }
}

export default Neditor;
//<Ueditor name={data['name']} content={data['value']} handleChange={this.editorChange.bind(this,data)} />